package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.PlaceExportDTO;
import net.wanho.po.Place;
import net.wanho.service.PlaceService;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/2/5 13:47
 * @version: 1.0
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("place")
public class PlaceController {
    // 产地
    private final PlaceService placeService;

    // 产地列表
    @GetMapping("place-list")
    public R placeList() {
        return R.ok(placeService.list());
    }

    // 所有产地列表
    @GetMapping("place-page-list")
    public R placeList(@RequestParam(required = false, defaultValue = "1") int pageNum,
                       @RequestParam(required = false, defaultValue = "5") int pageSize,
                       Place place) {
        PageInfo<Place> list = placeService.getPlacePage(pageNum, pageSize, place);
        return R.ok(list);
    }

    // 检查产地是否唯一
    @GetMapping("/place-num-check")
    public R checkPlaceNum(Integer placeNum) {
        Place one = placeService.selectOneByPlaceNum(placeNum);
        if (one != null) {
            return R.error(false);
        }
        return R.ok(true);
    }

    // 添加产地
    @PostMapping("/place-add")
    public R addsPlace(@RequestBody Place place) {
        boolean save = placeService.save(place);
        if (save) {
            return R.ok("仓库添加成功！");
        }
        return R.error("添加失败");
    }

    // 修改产地
    @PutMapping("/place-update")
    public R updatePlace(@RequestBody Place place) {
        boolean update = placeService.updateById(place);
        if (update) {
            return R.ok("仓库修改成功！");
        }
        return R.error("修改失败");
    }

    // 删除产地
    @DeleteMapping("/place-delete/{id}")
    public R deletePlace(@PathVariable Integer id) {
        boolean delete = placeService.removeById(id);
        if (delete) {
            return R.ok("仓库删除成功！");
        }
        return R.error("删除失败");
    }

    // 导出数据
    @GetMapping("/exportTable")
    public R exportTable(PlaceExportDTO placeExportDTO) {
        List<Place> list = placeService.exportTable(placeExportDTO);
        return R.ok("导出成功", list);
    }
}
